package cd.cdyb.sms.controller;

import cd.cdyb.sms.domain.OssConfig;
import cd.cdyb.sms.exception.BusinessException;
import cd.cdyb.sms.service.OssConfigService;
import cd.cdyb.sms.utils.OssFileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.UUID;

/**
 * @Author: lilin
 * @CreateTime: 2024-05-14  10:20
 * @Description: oss
 */
@RestController
@RequestMapping("/image")
@Slf4j
public class ImageController {

    @Resource
    private OssConfigService ossConfigService;

    /**
     * 上传oss
     *
     * @return 文件地址
     */
    @RequestMapping("/uploadOSS")
    public String uploadOss(@RequestParam("file") MultipartFile file) {
        try {
            if (file.getSize() > 10 * 1024 * 1024) {
                throw new BusinessException("图片超出最大限制");
            }
            InputStream input = file.getInputStream();
            OssConfig ossConfig = ossConfigService.getByAccountId(301);
            String imageId = UUID.randomUUID().toString();
            return OssFileUtils.uploadByAliYun(input, ossConfig, imageId, "image/jpg");
        } catch (Exception e) {
            log.error("上传图片失败，原因{}", e.getMessage());
        }
        return null;
    }
}
